package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.RoleMenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {
    @Reference
    private RoleMenuService roleMenuService;

    @RequestMapping("/getUserMenu")
    public Result getMenu(HttpServletRequest request) {
        request.setAttribute("errorMessage", MessageConstant.GET_MENU_SUCCESS);
        //获取获取用户名
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userName = user.getUsername();
        System.out.println(userName);
        //根据得到的用户名获取用户角色菜单信息
        List<Map<String, Object>> roleMenu = roleMenuService.findRoleMenu(userName);
        return new Result(true, MessageConstant.GET_MENU_SUCCESS, roleMenu);
    }
}
